package com.example.demo.util;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBC_Demo {

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver" );
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    //连接数据库
    public static java.sql.Connection getConnection(){
        try {
            return DriverManager.getConnection("jdbc:mysql://172.18.92.57:3306/javaweb","rjgc21","rjgc21");
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }
    //查询语句对象
    public static java.sql.Statement getStatement(){
        try {
            return getConnection().createStatement();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) {
        try {
            java.sql.Statement statement = getStatement();
            java.sql.ResultSet resultSet=statement.executeQuery("select * from user");
            while (resultSet.next()){
                System.out.println(resultSet.getString("username"));
                System.out.println(resultSet.getString("password"));
            }
        }
        catch (SQLException e){
            e.printStackTrace();
        }
    }
}
